from datetime import datetime
from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()

class BaseTable(models.Model):
    """
    公共字段列
    """
    class Meta:
        abstract = True
        verbose_name = "公共字段表"
        db_table = 'BaseTable'

    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)

class Project(BaseTable):
    """
    项目信息表
    """

    class Meta:
        verbose_name = "项目信息"
        db_table = "Project"

    name = models.CharField("项目名称", unique=True, null=False, max_length=100)
    desc = models.CharField("简要介绍", max_length=100, null=False)
    responsible = models.CharField("创建人", max_length=20, null=False)


class Testcase(BaseTable):
    CASE_TYPE = (
        (1, "功能测试"),
        (2, "接口测试"),
        (3, "UI测试"),
        (4, "兼容测试"),
        (5, "安全测试")
    )

    CASE_PRIORITY = (
        (1, "高"),
        (2, "中"),
        (3, "低"),
        (4, "None"),
    )

    CASE_STATUS = (
        (1, "草稿"),
        (2, "待审核"),
        (3, "已审核"),
        (4, "未执行"),
        (5, "已执行"),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    for_product = models.CharField(max_length=30, verbose_name="所属产品")
    for_project = models.CharField(max_length=30, verbose_name="所属模块")
    case_title = models.CharField(max_length=50, verbose_name="用例标题")
    case_condition = models.CharField(max_length=200, verbose_name="前置条件")
    case_step = models.CharField(max_length=200, verbose_name="用例步骤")
    case_expected_results = models.CharField(max_length=200, verbose_name="预期结果")
    case_actual_results = models.CharField(max_length=200, null=True, blank=True, verbose_name="实际情况")
    case_keywords = models.CharField(max_length=20, verbose_name="关键词")
    case_priority = models.IntegerField(default=2, choices=CASE_PRIORITY, verbose_name="用例优先级")
    case_type = models.IntegerField(default=1, choices=CASE_TYPE, verbose_name="用例类型")
    case_status = models.IntegerField(default=1, choices=CASE_STATUS, verbose_name="用例状态")

    class Meta:
        verbose_name = "用例"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.case_title


class Apicase(BaseTable):
    """
    接口数据库
    """
    CASE_METHOD = (
        (1, "GET"),
        (2, "POST"),
        (3, "PUT"),
        (4, "DELETE"),
    )

    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    name = models.CharField(default="", max_length=20, verbose_name="用例名")
    url = models.URLField(default="", null=True, blank=True, max_length=100, verbose_name="请求地址")
    body = models.TextField(default="", null=True, blank=True, verbose_name="请求体")
    method = models.IntegerField(choices=CASE_METHOD, default=1, verbose_name="请求方法")
    headers = models.CharField(default="", null=True, blank=True, max_length=100, verbose_name="请求头")
    validate = models.CharField(default="", null=True, blank=True, max_length=100, verbose_name="预期结果")
    validate_type = models.CharField(default="", max_length=20, verbose_name="预期结果比对方式")
    decs = models.CharField(default="", max_length=200, verbose_name="用例描述")

    class Meta:
        verbose_name = "接口用例"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
